package com.yungu.swift.admin.route.operation;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminUuidParam;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.system.sys.model.dto.SysBusinessOrganizationDto;
import com.yungu.swift.system.sys.model.param.AdminCommissionParam;
import com.yungu.swift.system.sys.model.param.AdminOrganizationParam;
import com.yungu.swift.system.sys.model.param.AdminOrganizationResourceParam;
import com.yungu.swift.system.sys.service.SysBusinessOrganizationService;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : cuixiuyin
 * @date : 2019/6/20
 */
@RestController
@RequestMapping("/admin/v1/operation/organization")
@Api(value = "【运营管理】", tags = "区域（代理、公司）相关操作")
public class BusinessOrganizationAdmin {

    @Reference
    private SysBusinessOrganizationService sysBusinessOrganizationService;

    @RequestMapping(value = "/addBusiness", method = RequestMethod.POST)
    @ApiOperation(value = "区域（代理、公司）开通业务", httpMethod = "POST")
    public ResponseData<Boolean> addBusiness(@RequestBody AdminOrganizationParam businessOrganizationParam) {
        if (ParamUtil.isIllegal(businessOrganizationParam.getBusinessUuid(), businessOrganizationParam.getOrganizationUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        SysBusinessOrganizationDto businessOrganizationDto = new SysBusinessOrganizationDto();
        BeanUtils.copyProperties(businessOrganizationParam, businessOrganizationDto);
        businessOrganizationDto.setCreateBy(businessOrganizationParam.getUserUuid());
        return sysBusinessOrganizationService.add(businessOrganizationDto);
    }


    @RequestMapping(value = "/closeBusiness", method = RequestMethod.POST)
    @ApiOperation(value = "区域（代理、公司）关闭业务", httpMethod = "POST")
    public ResponseData<Boolean> closeBusiness(@RequestBody AdminUuidParam uuidParam) {
        if (ParamUtil.isIllegal(uuidParam.getUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return sysBusinessOrganizationService.delete(uuidParam.getUuid());
    }


    @RequestMapping(value = "/editCommission", method = RequestMethod.POST)
    @ApiOperation(value = "区域（代理、公司）编辑抽成", httpMethod = "POST")
    public ResponseData<Boolean> editCommission(@RequestBody AdminCommissionParam commissionParam) {
        if (ParamUtil.isIllegal(commissionParam.getUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        SysBusinessOrganizationDto regionalAgentDto = new SysBusinessOrganizationDto();
        BeanUtils.copyProperties(commissionParam, regionalAgentDto);
        regionalAgentDto.setUpdateBy(commissionParam.getUserUuid());
        regionalAgentDto.setStatus(CommonConstant.STATUS_OPENED);
        return this.sysBusinessOrganizationService.edit(regionalAgentDto);
    }

    @RequestMapping(value = "/editResource", method = RequestMethod.POST)
    @ApiOperation(value = "区域（代理、公司）编辑资源", httpMethod = "POST")
    public ResponseData<Boolean> editResource(@RequestBody AdminOrganizationResourceParam resourceParam) {
        if (ParamUtil.isIllegal(resourceParam.getUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return sysBusinessOrganizationService.editResource(resourceParam.getUuid(), resourceParam.getResIds(),
                resourceParam.getUserUuid(), resourceParam.getBusinessType());
    }


    @RequestMapping(value = "/pointSite/editResource", method = RequestMethod.POST)
    @ApiOperation(value = "区域（代理、公司）编辑起终点资源", httpMethod = "POST")
    public ResponseData<Boolean> pointSiteEditResource(@RequestBody AdminOrganizationResourceParam resourceParam) {
        if (ParamUtil.isIllegal(resourceParam.getUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return sysBusinessOrganizationService.pointSiteEditResource(resourceParam.getUuid(), resourceParam.getResIds());
    }
}
